from link_list.node import SinglyListNode


def reverseBetween(head: SinglyListNode, m, n) -> SinglyListNode:
    curTemp = SinglyListNode(0)
    curTemp.next = head
    head = curTemp
    nexTemp = None
    preTemp = None
    i = 0
    while n > m:
        curTemp = head

        while i < m - 1:
            curTemp = curTemp.next
            i += 1
        preTemp = curTemp
        curTemp = curTemp.next
        nexTemp = curTemp.next

        i = 0
        while i < n - m:
            preTemp.next = nexTemp
            curTemp.next = nexTemp.next
            nexTemp.next = curTemp
            preTemp = preTemp.next
            nexTemp = curTemp.next
            i += 1
        n -= 1
        i = 0

    return head.next


first = SinglyListNode(1)
first.next = SinglyListNode(2)
first.next.next = SinglyListNode(3)
first.next.next.next = SinglyListNode(4)
first.next.next.next.next = SinglyListNode(5)
result = reverseBetween(first, 2, 4)
while result is not None:
    # 原顺序3215879
    print(result.val, end="")
    result = result.next
